package com.qianxunclub.context.Model;

import com.qianxunclub.util.BaseResponse;
import com.qianxunclub.util.ReturnCode;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.io.Serializable;
import java.util.Set;

/**
 * Created by zhangbin on 2017/3/10.
 */
public class BaseParam<ParamT> extends PageParam {

    private ParamT param;

    public BaseResponse validate(){
        param = (ParamT)this;
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        BaseResponse baseResponse = new BaseResponse();
        String message = null;
        Integer code = ReturnCode.CODE_1000.code();
        Set<ConstraintViolation<ParamT>> constraintViolations  = validator.validate(param);
        for(ConstraintViolation<ParamT> constraintViolation : constraintViolations) {
            message = constraintViolation.getMessage();
            code = ReturnCode.CODE_1004.code();
            break;
        }
        baseResponse.setReturnCode(code);
        baseResponse.setMessage(message);
        return baseResponse;
    }

    protected Serializable pkVal() {
        return this.hashCode();
    }
}
